package com.hunlian.makelove;

import android.net.NetworkInfo;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.github.pwittchen.reactivenetwork.library.Connectivity;
import com.github.pwittchen.reactivenetwork.library.ReactiveNetwork;
import com.hunlian.makelove.common.AppUtils;
import com.hunlian.makelove.net.CustomerOkHttpClient;
import com.hunlian.makelove.tools.MD5Tools;
import com.tencent.stat.StatService;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 
 * @author zealjiang
 * 
 */
public class AppApplication extends com.mob.MobApplication {
	private static AppApplication appContext;
	public static String SIGNMD5;
	public static final String TAG = "com.hunlian.makelove";
	public static StringBuilder errorTrack = new StringBuilder();
	/**
	 * 判断是否有网络
	 */
	public static boolean networkAvailable = true;



	@Override
	public void onCreate() {
		super.onCreate();
		Fresco.initialize(this);
		// 捕获崩溃异常，在开发调试阶段注释掉，发布时放出来
		Thread.setDefaultUncaughtExceptionHandler(new AppUncaughtExceptionHandler(this.getApplicationContext()));
		appContext = this;

		//腾讯MTA
		// 请在初始化时调用，参数为Application或Activity或Service
		StatService.setContext(appContext);
		// 自动监控Activity生命周期，可以代替之前手动调用的onResume和onPause，防止漏打点的情况
		StatService.registerActivityLifecycleCallbacks(appContext);

		String sign = AppUtils.getSign(this);
		MD5Tools md5Tools = MD5Tools.getInstance();
		String md5 = md5Tools.getMD5(sign);
		SIGNMD5 = "3441ab925e23cf0f23303ec8d6f1e0ae";//md5;
		//System.out.println("SIGNMD5: "+SIGNMD5);
		CustomerOkHttpClient.getClient();
		initNetworkStatusDetector();
	}

	public static AppApplication getAppContext() {
		return appContext;
	}

	/**
	 * 初始化网络监听
	 */
	public void initNetworkStatusDetector() {

		ReactiveNetwork.observeNetworkConnectivity(this)
				.subscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.subscribe(new Action1<Connectivity>() {
					@Override
					public void call(Connectivity connectivity) {
						//判断当前网络状态是否有效

						if (connectivity.getState() == NetworkInfo.State.CONNECTED) {
							//Toast.makeText(getApplicationContext(), "有网络", Toast.LENGTH_SHORT).show();
							networkAvailable = true;
						} else {
							//Toast.makeText(getApplicationContext(), "没有网络", Toast.LENGTH_SHORT).show();
							networkAvailable = false;
						}

/*						//判断当前网络状态
						if ((connectivityStatus.toString().equals(ConnectivityStatus.WIFI_CONNECTED.toString()) && connectivityStatus.toString().equals(ConnectivityStatus.WIFI_CONNECTED_HAS_NO_INTERNET.toString()))
								|| connectivityStatus.toString().equals(ConnectivityStatus.OFFLINE.toString())) {
							isWifi = NetStatus.WIFI_NO_INTERNET; //连接了wifi,且无线不能用 || 网络不可用
						} else if (connectivityStatus.toString().equals(ConnectivityStatus.MOBILE_CONNECTED.toString())) {
							isWifi = NetStatus.MOBILE_INTERNET; //连接了移动网络
						} else {
							isWifi = NetStatus.WIFI;//wifi网络
						}*/
					}
				});

/*		ReactiveNetwork.observeNetworkConnectivity(this)
				.subscribeOn(Schedulers.io())
				.filter(ConnectivityPredicate.hasState(NetworkInfo.State.CONNECTED))
				.filter(ConnectivityPredicate.hasState(NetworkInfo.State.DISCONNECTED))
				.filter(ConnectivityPredicate.hasType(ConnectivityManager.TYPE_WIFI))
				.observeOn(AndroidSchedulers.mainThread())
				.subscribe(new Action1<Connectivity>() {
					@Override
					public void call(Connectivity connectivity) {
						Toast.makeText(getApplicationContext(), "WIFI 连接上", Toast.LENGTH_SHORT).show();
					}
				});

		ReactiveWifi.observeWifiStateChange(this)
				.subscribeOn(Schedulers.io())
    			.observeOn(AndroidSchedulers.mainThread())
				.subscribe(new Action1<WifiState>() {
					@Override public void call(WifiState wifiState) {
						// do something with state
					}
				});*/
	}

}
